/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.javaeye.store.dto.Materials;
import com.javaeye.order.dto.PurchaseOrder;
import com.javaeye.order.dto.PurchaseOrderDetail;
import com.javaeye.procurement.dto.MaterialsRequest;
import com.javaeye.common.service.ICodeService;
import com.javaeye.store.service.IMaterialsService;
import com.javaeye.order.service.IPurchaseOrderService;
import com.javaeye.procurement.service.IMaterialsRequestService;
import com.javaeye.common.util.ListUtil;
import com.opensymphony.xwork2.ActionSupport;

public class MaterialsRequestAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6000362880537590517L;
	
	/**
	 * 采购需求列表
	 */
	private List<MaterialsRequest> requestList;
	
	/**
	 * 采购需求列表查询条件
	 */
	private MaterialsRequest condition;
	
	/**
	 * 订单ID
	 */
	private String orderId;
	
	/**
	 * 订单信息
	 */
	private PurchaseOrder order;
	
	/**
	 * 物料ID
	 */
	private List<Integer> materialsIds;
	
	/**
	 * 物料名称
	 */
	private List<String> materialsNames;
	
	/**
	 * 计划数量
	 */
	private List<Double> planNumbers;
	
	/**
	 * 预计完成时间
	 */
	private List<Date> finishDates;
	
	/**
	 * 分类
	 */
	private List<String> materialsCategorys;
	
	
	private IPurchaseOrderService orderService;
	
	private ICodeService codeService;
	
	private IMaterialsService materialsService;
	
	private IMaterialsRequestService service;
	
	public MaterialsRequestAction() {
		condition = new MaterialsRequest();
		condition.setOrderId("");
		condition.setCode("");
	}
	
	public void setOrderService(IPurchaseOrderService orderService) {
		this.orderService = orderService;
	}
	
	public void setCodeService(ICodeService codeService) {
		this.codeService = codeService;
	}
	
	public void setMaterialsService(IMaterialsService materialsService) {
		this.materialsService = materialsService;
	}

	public void setService(IMaterialsRequestService service) {
		this.service = service;
	}
	
	/**
	 * 查询待采购的物料需求列表
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsRequestList() throws Exception {
		requestList = service.getWaitingMaterialsRequests(condition);
		return SUCCESS;
	}

	/**
	 * 查询用于新建采购需求单
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsRequestBaseInfo() throws Exception {
		order = orderService.getPurchaseOrderInfo(orderId);
		
		order = orderService.getPurchaseOrderInfo(orderId);
		Set<PurchaseOrderDetail> details = order.getOrderDetails();
		for (PurchaseOrderDetail detail : details) {
			detail.setUnit(ListUtil.unitName(codeService, detail.getUnit()));
		}
		condition.setOrderId(orderId);
		requestList = service.getWaitingMaterialsRequests(condition);
		
		return SUCCESS;
	}
	
	/**
	 * 保存采购需求单
	 * @return
	 * @throws Exception
	 */
	public String saveMaterialsRequestList() throws Exception {
		List<MaterialsRequest> requests = new ArrayList<MaterialsRequest>();
		for (int i = 0; i < materialsIds.size(); i++) {
			int materialsId = materialsIds.get(i);
			MaterialsRequest mRequest = new MaterialsRequest();
			Materials materials = materialsService.getMaterials(String.valueOf(materialsId));
			mRequest.setMaterialsId(materialsId);
			mRequest.setOrderId(orderId);
			mRequest.setCode(materials.getCode());
			mRequest.setCategory(materialsCategorys.get(i));
			mRequest.setName(materials.getName());
			mRequest.setUnits(materials.getUnits());
			mRequest.setProperty(materials.getProperty());
			mRequest.setPlanNumber(planNumbers.get(i));
			mRequest.setFinishDate(finishDates.get(i));
			requests.add(mRequest);
		}
		service.saveMaterialsRequests(requests, orderId);
		return SUCCESS;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public PurchaseOrder getOrder() {
		return order;
	}

	public void setOrder(PurchaseOrder order) {
		this.order = order;
	}

	public List<Integer> getMaterialsIds() {
		return materialsIds;
	}

	public void setMaterialsIds(List<Integer> materialsIds) {
		this.materialsIds = materialsIds;
	}

	public List<String> getMaterialsNames() {
		return materialsNames;
	}

	public void setMaterialsNames(List<String> materialsNames) {
		this.materialsNames = materialsNames;
	}

	public List<Double> getPlanNumbers() {
		return planNumbers;
	}

	public void setPlanNumbers(List<Double> planNumbers) {
		this.planNumbers = planNumbers;
	}

	public List<Date> getFinishDates() {
		return finishDates;
	}

	public void setFinishDates(List<Date> finishDates) {
		this.finishDates = finishDates;
	}

	public List<String> getMaterialsCategorys() {
		return materialsCategorys;
	}

	public void setMaterialsCategorys(List<String> materialsCategorys) {
		this.materialsCategorys = materialsCategorys;
	}

	public void setRequestList(List<MaterialsRequest> requestList) {
		this.requestList = requestList;
	}

	public List<MaterialsRequest> getRequestList() {
		return requestList;
	}

	public void setCondition(MaterialsRequest condition) {
		this.condition = condition;
	}

	public MaterialsRequest getCondition() {
		return condition;
	}
	
}
